package com.codeoss.framework.web;

import java.io.File;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.codeoss.module.ir.service.IIrSchedulerService;

@Component
public class WebInitUtil {
	@Value("${framework.base-path}")
	private String basePath;
	@Autowired
	private IIrSchedulerService irSchedulerService;
	public  void initBeanUtils() {
		BigDecimalConverter bd = new BigDecimalConverter(BigDecimal.ZERO);
		ConvertUtils.register(bd, java.math.BigDecimal.class);

		SqlTimeConverter timeConverter = new SqlTimeConverter(null);
		ConvertUtils.register(timeConverter, Time.class);

		SqlDateConverter dateConverter = new SqlDateConverter(null);
		ConvertUtils.register(dateConverter, Date.class);

		SqlTimestampConverter datetimeConverter = new SqlTimestampConverter(null);
		ConvertUtils.register(datetimeConverter, Timestamp.class);

		LongConverter longConverter = new LongConverter(null);
		ConvertUtils.register(longConverter, Long.class);

		IntegerConverter integerConverter = new IntegerConverter(null);
		ConvertUtils.register(integerConverter, Integer.class);
	}
	
	public void createResourceFolder(){
		File basePathFolder=new File(basePath);
		if(!basePathFolder.exists()) {
			basePathFolder.mkdirs();
		}
		File attachmentFolder=new File(basePathFolder,"attachment");
		if(!attachmentFolder.exists()) {
			attachmentFolder.mkdir();
		}
		File codeFolder=new File(basePathFolder,"code");
		if(!codeFolder.exists()) {
			codeFolder.mkdir();
		}
		File tempFileFolder=new File(basePathFolder,"tempfile");
		if(!tempFileFolder.exists()) {
			tempFileFolder.mkdir();
		}
	}
	
	public void init() {
		initBeanUtils();
		createResourceFolder();
		try {
			irSchedulerService.boot();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}
	}
}
